/* internal/js/delivery/view.js */

/* Common */

var SERVLET_PATH = '/ordrs/internal/maintain-delivery'; 

/* END Common */


/* Report */

function reportDelivery () {
	reportDeliveryValidate();
}

//done
function reportDeliveryValidate () {
	var firstname =  document.getElementById('firstname').value;
	var lastname = document.getElementById('lastname').value;
	var phone = document.getElementById('phone').value;

	reportDeliverySend(firstname, lastname, phone);
}

//done
function reportDeliverySend (firstname, lastname, phone) {
	var url = SERVLET_PATH;
	var params = 'a=' + encodeURI('r') +
				'&firstname=' + encodeURI(firstname) +
	            '&lastname=' + encodeURI(lastname) +
		        '&phone=' + encodeURI(phone);

	var req = getXmlHttpObject();
	req.onreadystatechange = function() {
		if (req.readyState == 4) {
			if (req.status == 200) {
				//returned code
				reportDeliveryReceive(req.responseXML);
			} else if (req.status == 204) {
				//can't find server
				reportDeliveryReceive(encodeErrorXML('-1', 'Cannot find server.'));
			}
		}
	};
	sendPost(req, url, params);
}

function reportDeliveryReceive (xml) {
	if (xml.getElementsByTagName('deliveries').length > 0) {
		var deliveries = xml.getElementsByTagName('deliveries')[0];
		showResults(buildTable(deliveries));
		if (deliveries.childNodes.length == 1)
		{
			showResult(deliveries.childNodes[0].getElementsByTagName('id')[0].childNodes[0].nodeValue);
		}
	} else {
		var code = getErrorCode(xml);
		var message = getErrorMessage(xml);
		if (code == 10101)
			showResults(message);
		else if (code == -1)
			showDialog(message);
		else
			showDialog('Error ' + getErrorString(xml));
	}
}

/* END Report*/


/* List All */

function listAllDelivery () {
	listAllDeliverySend();
}

function listAllDeliverySend () {
	var url = SERVLET_PATH;
	var params = 'a=' + encodeURI('l');

	var req = getXmlHttpObject();
	req.onreadystatechange = function() {
		if (req.readyState == 4) {
			if (req.status == 200) {
				//returned code
				listAllDeliveryReceive(req.responseXML);
			} else if (req.status == 204) {
				//can't find server
				listAllDeliveryReceive(encodeErrorXML('-1', 'Cannot find server.'));
			}
		}
	};
	sendPost(req, url, params);
}


function listAllDeliveryReceive (xml) {
	if (xml.getElementsByTagName('deliveries').length > 0) {
		var deliveries = xml.getElementsByTagName('deliveries')[0];
		showResults(buildTable(deliveries));
	} else {
		var code = getErrorCode(xml);
		var message = getErrorMessage(xml);
		if (code == 10201)
			showResults(message);
		else if (code == -1)
			showDialog(message);
		else
			showDialog('Error ' + getErrorString(xml));
	}
}

/* END List All */



/* Flag as complete */

function flagAsComplete (id) {
	// get of the info of delivery from hidden input
	var frm = document.getElementById('listDeliveryForm' + id);
	
	var firstname = frm.firstname.value;
	var lastname = frm.lastname.value;
	var phone = frm.phone.value;
	var address = frm.address.value;
	var city = frm.city.value;
	var province = frm.province.value;
	var pos = frm.postalCode.value;
	var dwelling = frm.dwelling.value;
	var room = frm.room.value;
	var building = frm.room.value;
	var status;
	if (frm.status.value != 'Done')
		status = 'Done';
	else
		status = 'Not Ready';
	var instruction = frm.instruction.value;
	
	// update the delivery with the status 'Done'
	updateDeliverySend (id, firstname, lastname, phone, address, city,
			province, pos, dwelling, status, building, room, instruction);
}

/* Update */

function updateDeliverySend (id, firstname, lastname, phone, address, city,
		province, postalCode, dwelling, status, building, room, instruction) {
	var url = SERVLET_PATH;
	var params = 'a=' + encodeURI('u') +
	            '&id=' + encodeURI(id) +
	            '&firstname=' + encodeURI(firstname) +
	            '&lastname=' + encodeURI(lastname) +
	            '&phone=' + encodeURI(phone) +
	            '&address=' + encodeURI(address) +
	            '&city=' + encodeURI(city) +
	            '&province=' + encodeURI(province) +
	            '&postal=' + encodeURI(postalCode) +
	            '&dwelling=' + encodeURI(dwelling) +
	            '&building=' + encodeURI(building) +
	            '&room=' + encodeURI(room) +
	            '&status=' + encodeURI(status) +
	            '&instruction=' + encodeURI(instruction);
	
	var req = getXmlHttpObject();
	req.onreadystatechange = function() {
		if (req.readyState == 4) {
			if (req.status == 200) {
				//returned code
				updateDeliveryReceive(req.responseXML);
			} else if (req.status == 204) {
				//can't find server
				updateDeliveryReceive(encodeErrorXML('-1', 'Cannot find server.'), false);
			}
		}
	};
	sendPost(req, url, params);
}


function updateDeliveryReceive (xml) {
	var code = getErrorCode(xml);
	var message = getErrorMessage(xml);
	if (code == 0) {
		listAllDeliverySend();
	} else if (code == -1)
		showDialog(message);
	else
		showDialog('Error ' + getErrorString(xml));
}

/* END Flas as complete */